package com.baibei.order.bill.sticky_header;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baibei.model.DateInfo;
import com.baibei.order.R;
import com.baibei.order.bill.viewholder.DateViewHolder;
import com.baibei.order.bill.viewtype.ViewType;
import com.baibei.order.emptyview.EmptyViewHolder;
import com.blankj.utilcode.utils.LogUtils;
import com.blankj.utilcode.utils.TimeUtils;

import java.util.List;

/**
 * 基础adapter,用于滚动头部
 * Created by shenminjie on 2017/8/9.
 * mail:shenminjie@100bei.com
 */

public abstract class StickyDateAdapter<T extends DateInfo> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 数据源
     */
    protected List<T> mDataInfos;

    /**
     * 分别用index记录日期与数据源
     */
    protected SparseArray<String> mDates;
    protected SparseArray<T> mRecordObjs;


    public StickyDateAdapter() {
        mDates = new SparseArray<>();
        mRecordObjs = new SparseArray<>();
    }


    /**
     * 设置数据
     *
     * @param infos infos
     */
    public void setInfos(List<T> infos) {
        mDataInfos = infos;
        updateData();
    }
    private boolean center;
    public void setToptextGravity(){
        center=true;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ViewType.DATE.ordinal()) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sticky_header_date, parent, false);
            TextView date= view.findViewById(R.id.tv_date);
            if (center){
                date.setGravity(Gravity.CENTER);
            }
            return new DateViewHolder(view);
        }
        if (viewType == ViewType.EMPTY.ordinal()) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_empty_view, parent, false);
            return new EmptyViewHolder(view);
        }
        return getItemViewHolder(parent, viewType);
    }

    /**
     * 获取item viewHolder
     *
     * @return return
     */
    public abstract RecyclerView.ViewHolder getItemViewHolder(ViewGroup parent, int viewType);

    /**
     * 处理itemviewHolder
     *
     * @param holder   holder
     * @param position position
     */
    public abstract void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position);

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //空视图不处理
        if (holder instanceof EmptyViewHolder) {
            onBindEmptyView((EmptyViewHolder) holder);
            return;
        }
        if (holder instanceof DateViewHolder) {
            String date = mDates.get(position);
            holder.itemView.setTag(ViewType.DATE.name());
            if (position==0){
                ((DateViewHolder) holder).getTvDate().setTextColor(Color.parseColor("#333333"));
            }else{
                ((DateViewHolder) holder).getTvDate().setTextColor(Color.parseColor("#666666"));
            }
            holder.itemView.setContentDescription(date);
            DateViewHolder viewHolder = (DateViewHolder) holder;
            viewHolder.getTvDate().setText(date);
        } else {
            T info = mRecordObjs.get(position);
            holder.itemView.setTag(ViewType.ITEM.name());
            holder.itemView.setContentDescription(getDate(info.getDate()));
            onBindItemViewHolder(holder, position);
        }
    }

    /**
     * 处理空视图
     *
     * @param holder holder
     */
    private void onBindEmptyView(EmptyViewHolder holder) {
        //nothing to do
    }

    @Override
    public int getItemCount() {
        if (getDataCount() == 0) {
            return 1;
        }
        return getDataCount();
    }

    /**
     * 获取数据集合总长度
     *
     * @return return
     */
    public int getDataCount() {
        return mDates.size() + mRecordObjs.size();
    }


    @Override
    public int getItemViewType(int position) {
        //空视图
        if (getDataCount() == 0) {
            return ViewType.EMPTY.ordinal();
        }
        if (mDates.get(position) != null) {
            return ViewType.DATE.ordinal();
        }
        if (mRecordObjs.get(position) != null) {
            return ViewType.ITEM.ordinal();
        }
        return super.getItemViewType(position);
    }


    /**
     * 逻辑判断位置
     */
    public void updateData() {
        if (mDataInfos == null || mDataInfos.size() == 0) {
            return;
        }
        mDates.clear();
        mRecordObjs.clear();
        int size = 0;
        for (int i = 0; i < mDataInfos.size(); i++) {
            //当前的对象
            T cur = mDataInfos.get(i);
            String curDate = getDate(cur.getDate());
            if (i != 0) {
                T pre = mDataInfos.get(i - 1);
                String preDate = getDate(pre.getDate());
                if (!curDate.equals(preDate)) {
                    size++;
                    mDates.put(size - 1, curDate);
                }
            } else {
                size++;
                mDates.put(size - 1, curDate);
            }
            size++;
            mRecordObjs.put(size - 1, cur);
        }
    }

    /**
     * 获取日期
     *
     * @param mill mill
     * @return return
     */
    private String getDate(long mill) {
//        LogUtils.e("tags",mill);
        return TimeUtils.millis2String(mill, "yyyy年MM月dd日");
    }


}
